Ext.define('GBIF.ProjectWindow', {
		extend: 'Ext.window.Window'	
	,	alias: 'widget.projectWindow'
	,	width: 520
	,	height: 320
	,	iconCls: 'icon_gbif'
	,	layout: 'fit'
	,	closable: false
	,	draggable: false
	,	resizable: false
	,	modal: true
	,	listeners: {
				show: function(){
					Ext.WindowMgr.mask.addCls('x-mask-intro');
				}
			,	destroy: function(){
					Ext.WindowMgr.mask.removeCls('x-mask-intro')
				}
		}
	,	initComponent: function() {
			this.tbar = [{
					text: this.txtHelp
				,	iconCls: 'icon_help'	
				,	menu: {
						items: [{
								text: this.txtAbout
							,	iconCls: 'icon_information'
							,	scope: this
							,	handler: function() {									
									Ext.Msg.alert(this.msgAboutTitle, this.msgAboutText);
								}
						}, {
								text: this.txtGettingStarted
							,	iconCls: 'icon_page'	
							,	handler: function() { 
									window.open(Config.getStartedUrl, '_blank');
								}
						}, {
								text: this.txtProjectWebsite
							,	iconCls: 'icon_page'		
							,	handler: function() { 
									window.open(Config.projectUrl, '_blank');
								}
						}, {
								text: this.txtReportIssue
							,	iconCls: 'icon_page'
							,	scope: this	
							,	handler: function() { 
									window.open(Config.issuesUrl, '_blank');
								}
						}]
					}
				}, '->',{
						xtype: 'label'
					,	text: this.txtLanguage
				}, {
						xtype: 'combo'  
					,	store: language
					,	queryMode: 'local'
					,	value: lang
					,	displayField: 'value'
					,	valueField: 'id'
					,	triggerAction: 'all'
					,	editable: false
					,	width: 100
					,	listeners: {
							select: function(f, r, i){
								var lang = r[0].data.id;
								window.location ='?lang=' + lang;
							}
						}
			}];
			this.projectCode = Ext.create('Ext.Button', {
					text: this.txtLoadProject
				,	style: 'margin-bottom: 5px;'
				,	scope: this
				,	handler: function(){
						this.openProject();
					}
			});
			this.createCode = Ext.create('Ext.Button', {
					text: this.txtCreateProject
				,	style: 'margin-bottom: 5px;'
				,	scope: this
				,	handler: function(){
						this.createProject();
					}
			});
			this.pidForm = Ext.create('Ext.form.Panel', {
					border: false
				,	defaultType: 'textfield'
				,	bodyPadding: 10
				,	fieldDefaults: {
							labelAlign: 'top'
						,	labelWidth: 100
					}
				,	items: [{
								xtype: 'displayfield'
							,	value: '<div style="padding-bottom:5px;">'+ this.msgLoadProjectInfo +'</div>'
						}, {
								name: 'projectcode'
							,	fieldLabel: this.txtProjectCode
							,	anchor: '100%'
							,	id: 'projectCode'
						}, this.projectCode
						, {
								xtype: 'displayfield'
							,	value: '<div style="padding-bottom:5px;">'+ this.msgCreateProjectInfo +'</div>'
						}, {
								fieldLabel: this.txtDCAUrl
							,	name: 'coreurl'
							,	anchor: '100%'
						}, this.createCode ]
			});
			this.items = [ this.pidForm ]
			this.callParent();
		}
	,	openProject: function(){
			var value = this.pidForm.getForm().getValues();
			var projectcode = value.projectcode;
			if(!Ext.isEmpty(projectcode) && Ext.isDefined(projectcode)){
				 Ext.Ajax.request({
						url: Config.baseUrl + 'resources/api/api.php'
					,	params: {
								cmd: 'getStatus'
							,	projectId: projectcode
						}
					,	method: 'POST'
					,	scope: this
					,	success: function (response) {
							var o = Ext.decode(response.responseText);
							if(o.success){
								this.setProjectID(projectcode);
								GBIF.currentArchiveUrl = '';
								this.setStatus(o.status);
								if(o.status == "ARCHIVED"){
									GBIF.currentArchiveUrl = o.archiveUrl;
								}
								if(o.status == "ERROR"){
									GBIF.currentErrorCode = o.comments
								} 
								this.close();
							} else {
								GBIF.Notice.msg(this.msgErrorTitle,o.error.msg);
								this.setFocus(1);
							}
						}
					,	failure: function () {
							GBIF.Notice.msg(this.msgErrorTitle, this.msgFatalError);
							this.setFocus(1);
						}
				});
			} else {
				GBIF.Notice.msg(this.msgNoticeTitle, this.msgValidProjectCode);
				this.setFocus(1);
			}
		}
	,	isURL: function(s) {
			var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
			return regexp.test(s);
		}
	,	createProject: function(){
			var value = this.pidForm.getForm().getValues();
			var coreurl = value.coreurl.trim();
			if(!Ext.isEmpty(coreurl) && Ext.isDefined(coreurl) && this.isURL(coreurl)){
					Ext.Ajax.request({
							url: Config.baseUrl + 'resources/api/api.php'
						,	params: {
									cmd: 'addProject'
								,	url: coreurl
							}
						,	method: 'POST'
						,	scope: this
						,	success: function (response) {
								var o = Ext.decode(response.responseText);
								if(o.success){
									this.setProjectID(o.projectId);
									this.setStatus(o.status);
									this.close();
								} else {
									GBIF.Notice.msg(this.msgErrorTitle, o.error.msg);
									this.setFocus(4);
								}
							}
						,	failure: function () {
								GBIF.Notice.msg(this.msgErrorTitle, this.msgFatalError);
								this.setFocus(4);
							}
					});
			} else {
				GBIF.Notice.msg(this.msgNoticeTitle, this.msgValidUrl);
				this.setFocus(4);
			}
		}
	,	setFocus: function(itemid){
			this.pidForm.getForm().reset();
			this.pidForm.items.items[itemid].focus(true, 10);
		}	
	,	getProjectID: function(){
			return this.currentProjectID;
		}
	,	setProjectID: function(pid){
			this.currentProjectID = pid;
		}
	,	getStatus: function(){
			return this.currentStatus;
		}
	,	setStatus: function(status){
			this.currentStatus = status;
		}
});